package admin.data;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;

import models.CoordinaModel;
import models.OrderProductModel;
import models.StoreItemModel;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyOpts;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

public class CoordinaData {
	private Objectify ofy;

	private Cache cache = null;

	private String prefix = "Coordina_";

	private String dealId = null;

	private String dealName = null;
	
	private TreeMap<String, OrderProductModel> products = new TreeMap<String, OrderProductModel>();

	private int storeType = 0;

	private String listOrderId = null;

	private int orderNumber = 0;	
	
	private String productId = null;

	private String productName = null;
	
	private int quantity = 0;
	
	private int inventory = 0;
	
	public void addProduct(OrderProductModel product)
	{
		if(products.containsKey(product.getProductId()))
		{
			OrderProductModel pro = products.get(product.getProductId());
			pro.setQuantity(pro.getQuantity()+product.getQuantity());
			products.put(product.getProductId(), pro); 
		}
		else
		{
			products.put(product.getProductId(), product); 
		}		
	}
	
	public String getDealId() {
		return dealId;
	}
	
	public void setDealId(String dealId) {
		this.dealId = dealId;
	}

	public String getDealName() {
		return dealName;
	}

	public void setDealName(String dealName) {
		this.dealName = dealName;
	}

	public TreeMap<String, OrderProductModel> getProducts() {
		return products;
	}

	public void setProducts(TreeMap<String, OrderProductModel> products) {
		this.products = products;
	}

	public int getStoreType() {
		return storeType;
	}

	public void setStoreType(int storeType) {
		this.storeType = storeType;
	}

	public String getListOrderId() {
		return listOrderId;
	}

	public void setListOrderId(String listOrderId) {
		this.listOrderId = listOrderId;
	}

	public int getOrderNumber() {
		return orderNumber;
	}

	public void setOrderNumber(int orderNumber) {
		this.orderNumber = orderNumber;
	}
	
	public String getProductId() {
		return productId;
	}

	public void setProductId(String productId) {
		this.productId = productId;
	}

	public String getProductName() {
		return productName;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

	public int getQuantity() {
		return quantity;
	}

	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}

	public int getInventory() {
		return inventory;
	}

	public void setInventory(int inventory) {
		this.inventory = inventory;
	}

	public CoordinaData() {
		try {
			ObjectifyService.register(CoordinaModel.class);
		} catch (Exception e) {
			// e.printStackTrace();
		}

		try {
			cache = CacheManager.getInstance().getCacheFactory()
					.createCache(Collections.emptyMap());
		} catch (CacheException e) {
			// e.printStackTrace();
		}

		ObjectifyOpts opts = new ObjectifyOpts().setSessionCache(true);
		ofy = ObjectifyService.begin(opts);
	}

	public void insert(CoordinaModel coor) {
		ofy.put(coor);
	}

	public ArrayList<CoordinaModel> getCoordina(String routeId, String status,
			String startTime, String endTime, String staffId, String coordinaId) {
		ArrayList<CoordinaModel> list = new ArrayList<CoordinaModel>();
		Query<CoordinaModel> query = ofy.query(CoordinaModel.class);
		if (routeId.length() == 8) {
			query.filter("routeId", routeId);
		}
		if (status.length() == 1) {
			query.filter("status", Integer.parseInt(status));
		}

		DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
		try {
			Date start = formatter.parse(startTime);
			query.filter("created >", start);
		} catch (Exception e) {
		}
		try {
			Date end = formatter.parse(endTime);
			query.filter("created <", end);
		} catch (Exception e) {
		}

		if (staffId.length() > 5) {
			query.filter("employeeId", staffId);
		}
		if (coordinaId.length() > 8) {
			query.filter("id", coordinaId);
		}

		for (CoordinaModel coor : query) {
			list.add(coor);
		}
		return list;
	}

	public CoordinaModel getById(String id) {
		CoordinaModel coordinaModel = new CoordinaModel();
		try {
			coordinaModel = ofy.get(new Key<CoordinaModel>(CoordinaModel.class,
					id));
		} catch (Exception e) {
			coordinaModel = null;
		}
		return coordinaModel;
	}
}
